package messages;

/**
 * This is the general acknowledgement message for message,
 * it indicates the message type it is responding to, and an optional
 * success boolean
 * 
 * @author Ian Doerr
 */
public class GeneralAck extends BaseMessage
{
    private MessageType ackType;
    private boolean success;

    /**
     * Parses out a General Ack from the arguments
     * 
     * @param data
     */
    public GeneralAck(byte[] data)
    {
        super(data);

        this.ackType = MessageType.getMessageTypeForId(data[1]);
        this.success = data[2] == 1;
    }
    
    /**
     * Creates a GeneralAck object
     * @param ackType
     * @param success
     */
    public GeneralAck(MessageType ackType, boolean success)
    {
        super(null);
        this.ackType = ackType;
        this.success = success;
    }

    @Override
    /**
     * returns a byte[] representation of this message
     */
    public byte[] toByteArray()
    {
        byte[] retVal = new byte[getMessageSize()];
        retVal[0] = getMessageType().getIdentifier();
        retVal[1] = ackType.getIdentifier();
        retVal[2] = (byte) (success ? 1 : 0);
        
        return retVal;
    }

    @Override
    /**
     * returns the message type of this message
     */
    public MessageType getMessageType()
    {
        return MessageType.GeneralAck;
    }
    
    /**
     * returns the message type of the message that is 
     * being replied to.
     * 
     * @return
     */
    public MessageType getAckMessageType()
    {
        return ackType;
    }
    
    /**
     * returns the success argument of the message
     * 
     * @return
     */
    public boolean getSuccess()
    {
        return success;
    }

    @Override
    /**
     * returns the size of the message
     */
    public int getMessageSize()
    {
        return 3;
    }
    
}
